package test
import (
	"net/url"
	"io/ioutil"  
 	"net/http"
	 "encoding/json"  
	 "time"
	 "bytes"
	 "github.com/xinliangnote/go-gin-api/pkg/signature"
)
func GenerateSignature(path string, method string, params url.Values)(string, string, error){
	key := "dazi_backend"
	secret := "ff15c15950d639b34a95"
	ttl :=  time.Minute * 10
	authorization, date, err := signature.New(key, secret, ttl).Generate(path, method, params)
	return authorization, date, err
}
func Get(url string, header map[string] string) ([]byte, error){
	// 创建一个新的HTTP客户端  
	client := &http.Client{}  
	// 创建GET请求  
	req, err := http.NewRequest("GET", url, nil)  
	if err != nil {  
		return nil, err
	}  
	// 设置请求的Header字段  
	req.Header.Set("Authorization", header["Authorization"])  
	req.Header.Set("Authorization-Date", header["Authorization-Date"])  
	// 发送请求并获取响应  
	resp, err := client.Do(req)  
	if err != nil {  
		return nil, err
	}  
	defer resp.Body.Close()  
	// 读取响应体内容  
	body, err := ioutil.ReadAll(resp.Body)  
	if err != nil {  
		return nil, err  
	}  
	return body, nil
}

func Post(url string, header map[string] string, body map[string]interface{}) ([]byte, error){
	// 创建一个新的HTTP客户端  
	client := &http.Client{} 
	jsonData, err := json.Marshal(body)  
	if err != nil {   
		return  nil, err
	}  
 	// 创建一个带有JSON数据的HTTP请求体  
 	reqBody := bytes.NewBuffer(jsonData)   
	// 创建POST请求  
	req, err := http.NewRequest("POST", url, reqBody)  
	if err != nil {  
		return nil, err
	}  
	// 设置请求的Header字段  
	req.Header.Set("Content-Type", header["Content-Type"])  
	req.Header.Set("Authorization", header["Authorization"])  
	req.Header.Set("Authorization-Date", header["Authorization-Date"])  
	// 发送请求并获取响应  
	resp, err := client.Do(req)  
	if err != nil {  
		return nil, err
	}  
	defer resp.Body.Close()  
	// 读取响应体内容  
	res_body, err := ioutil.ReadAll(resp.Body)  
	if err != nil {  
		return nil, err  
	}  
	return res_body, nil
}